//
//  SharedPrefs.m
//  MobilIzleme
//
//  Created by Mehmet Sinan Şahin on 1/7/12.
//  Copyright (c) 2012 netgis. All rights reserved.
//

#import "SharedPrefs.h"
#import "NGUtility.h"

@implementation SharedPrefs


+ (void) registerDefaults {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"YES" forKey:APP_SETTING_SEND_TO_SERVER]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"http://213.139.195.74/MTS/GenericClass/DataRetrievingService.asmx" forKey:APP_SETTING_SERVICE_URL]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"YES" forKey:APP_SETTING_USE_GPS]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"NO" forKey:APP_SETTING_SIGNIFICANT_LOCATION_UPDATE]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"1" forKey:APP_SETTING_SEND_FREQUENCY]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"10" forKey:APP_SETTING_LOCATION_SIZE]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"100" forKey:APP_SETTING_TIME_INTERVAL]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"1" forKey:APP_SETTING_DELTA_DISTANCE]];

    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"" forKey:APP_SETTING_USERNAME]];
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"" forKey:APP_SETTING_PASSWORD]];

    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"1" forKey:APP_SETTING_DB_MODEL_VERSION]];
    
    [defaults registerDefaults:[NSDictionary dictionaryWithObject:@"NO" forKey:APP_SETTING_SERVICE_AGGREMENT]];
    
    [defaults synchronize];
}

+ (void) sync {
    [[NSUserDefaults standardUserDefaults] synchronize];
    //TODO GPS'in restart olması gerekir.
}

+ (BOOL) useGPS {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:APP_SETTING_USE_GPS];
}

+ (void) setUseGPS: (BOOL)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setBool:value forKey:APP_SETTING_USE_GPS];    
}

+ (BOOL) isSendToServer {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:APP_SETTING_SEND_TO_SERVER]; 
}

+ (void) setSendToServer: (BOOL)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setBool:value forKey:APP_SETTING_SEND_TO_SERVER];       
}

+ (BOOL) isSignificant {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:APP_SETTING_SIGNIFICANT_LOCATION_UPDATE]; 
}

+ (void) setSignificantLocationUpdate: (BOOL)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setBool:value forKey:APP_SETTING_SIGNIFICANT_LOCATION_UPDATE]; 
}

+ (NSString *)getServiceUrl {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *value = [defaults stringForKey:APP_SETTING_SERVICE_URL]; 
    if ([NSString isEmpty:value]) {
        NSLog(@"SERVER URL OKUNAMADI!");
        return TEST_SERVER_URL;
    }
    else {
        NSLog(@"SERVER URL OKUNDU %@\n", value);
    }
        
    return value;
}

+ (NSString *)getServiceUrlHostName {
    NSURL *url = [NSURL URLWithString:[SharedPrefs getServiceUrl]];
    return  [url host];
}

+ (void)setServiceUrl:(NSString *)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:value forKey:APP_SETTING_SERVICE_URL];  
}
+ (NSString *)getSessionID {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults stringForKey:APP_SETTING_SESSION_ID];  
}

+ (void)setUserName:(NSString *)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:value forKey:APP_SETTING_USERNAME];  
}
+ (NSString *)getUserName {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults stringForKey:APP_SETTING_USERNAME];  
}

+ (void)setPassword:(NSString *)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:value forKey:APP_SETTING_PASSWORD];  
}
+ (NSString *)getPassword {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	return [defaults stringForKey:APP_SETTING_PASSWORD];  
}

+ (void)setSessionID:(NSString *)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setObject:value forKey:APP_SETTING_SESSION_ID];  
}

+ (int) getLocationSize {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:APP_SETTING_LOCATION_SIZE]; 
}

+ (void) setLocationSize: (int)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setInteger:value forKey:APP_SETTING_LOCATION_SIZE];  
}

+ (int) getDeltaDistance {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:APP_SETTING_DELTA_DISTANCE]; 
}

+ (void) setDeltaDistance: (int) value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setInteger:value forKey:APP_SETTING_DELTA_DISTANCE]; 
}

+ (CLLocationAccuracy) getTimeInterval {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    int value = [defaults integerForKey:APP_SETTING_TIME_INTERVAL]; 
    
    switch (value) {
        case BEST_ACCURACY_:
            return kCLLocationAccuracyBest;
            
        case BEST_ACCURACY_10M:
            return kCLLocationAccuracyNearestTenMeters;
            
        case NORMAL_ACCURACY:
            return kCLLocationAccuracyHundredMeters;
            
        case NEAR_ACCURACY_1KM:
            return kCLLocationAccuracyKilometer;
            
        case NEAR_ACCURACY_3KM:
            return kCLLocationAccuracyThreeKilometers;
            
        default:
            return kCLLocationAccuracyKilometer;
    }
}

+ (void) setTimeInterval: (int) value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setInteger:value forKey:APP_SETTING_TIME_INTERVAL];  
}


+ (int) getSendFrequency {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:APP_SETTING_SEND_FREQUENCY];     
}

+ (void) setSendFrequency: (int)value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setInteger:value forKey:APP_SETTING_SEND_FREQUENCY];  
}

+ (int) getDBModelVersion {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults integerForKey:APP_SETTING_DB_MODEL_VERSION];
}

+ (BOOL) isServiceAggreed {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults boolForKey:APP_SETTING_SERVICE_AGGREMENT]; 
}

+ (void) setServiceAggrementStatus: (BOOL) value {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
	[defaults setBool:value forKey:APP_SETTING_SERVICE_AGGREMENT]; 
}



@end
